<template>
  <div class="app-container">
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
      <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
      <div class="formTopFont">检验类别信息</div>
      <el-row>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
          <el-form-item label="上级分类" prop="parentId">
            <tree-select ref="treeSelect" style="width: 100%" :props="{
                value: 'id',
                label: 'classifyName',
                children: 'children'
              }" :options="classifyData" :disabled="false" :accordion="true" :value="ruleForm.parentId" search-key="classifyName" @getNode="getValue($event)" />
          </el-form-item>
          <el-form-item label="类别名称" prop="classifyName">
            <el-input v-model="ruleForm.classifyName" maxlength="20" show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="类别编码" prop="classifyCode">
            <el-input v-model="ruleForm.classifyCode" maxlength="20" show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="remark">
            <el-input v-model="ruleForm.remark" maxlength="200" type="textarea" rows="3" show-word-limit></el-input>
          </el-form-item>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  data() {
    const checkName = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/qm/check/classify/verifyNameUnique?classifyName=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('类别名称重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    const checkCode = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/qm/check/classify/verifyCodeUnique?classifyCode=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('类别编码重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      ruleForm: {
        classifyName: '',
        classifyCode: '',
        remark: '',
        parentId: '0',
        parentName: ''
      },
      rules: {
        parentId: [{ required: false, message: '请选择上级类别' }],
        classifyName: [
          { required: true, message: '类别名称不能为空！', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        classifyCode: [
          { required: true, message: '类别编码不能为空！', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      classifyData: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    showDialog(row, classifyData) {
      this.dialogVisible = true
      this.classifyData = [{ id: '0', classifyName: '无' }, ...classifyData]
      if (row) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 获取选中的上级部门
     * @param {*} value
     * @return {*}
     */
    getValue(value) {
      this.ruleForm.parentId = value.id
      this.ruleForm.parentName = value.deptName
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/qm/check/classify/submit',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        classifyName: '',
        classifyCode: '',
        remark: '',
        parentId: '0',
        parentName: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
